using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace kp
{
    public partial class ucData : UserControl
    {
        public class ColumnBeforeAddEventArgs : EventArgs
        {
            public DataColumn DataColumn;
            public DataGridViewColumn GridColumn;

            public ColumnBeforeAddEventArgs(DataColumn DataColumn, DataGridViewColumn GridColumn)
            {
                this.DataColumn = DataColumn;
                this.GridColumn = GridColumn;
            }
        }

        public delegate void ColumnBeforeAddEventHandler(object sender, ColumnBeforeAddEventArgs e);

        public event ColumnBeforeAddEventHandler ColumnBeforeAdd;

        protected virtual void OnColumnBeforeAdd(ColumnBeforeAddEventArgs e)
        {
            if (ColumnBeforeAdd != null)
                ColumnBeforeAdd(this, e);
        }

        private StringCollection settings;

        public ucData()
        {
            InitializeComponent();
        }

        [AttributeProvider(typeof(IListSource))]
        [DefaultValue("")]
        public object DataSource
        {
            get { return this.bs.DataSource; }
            set { this.bs.DataSource = value; }
        }

        [Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [DefaultValue("")]
        public string DataMember
        {
            get { return this.bs.DataMember; }
            set { this.bs.DataMember = value; }
        }

        [DefaultValue("")]
        public string Sort
        {
            get { return this.bs.Sort; }
            set { this.bs.Sort = value; }
        }

        //[Browsable(false)]
        //[EditorBrowsable(EditorBrowsableState.Always)]
        //[DesignerSerializationVisibility(1)]
        [SettingsBindable(true)]
        [DefaultValue(null)]
        public StringCollection Settings
        {
            get
            {
                if (!this.DesignMode && settings == null)
                    settings = new StringCollection();
                return this.settings;
            }
            set { this.settings = value; }
        }

        private void NotifyChange()
        {
            if (this.bs.DataSource == null
                || this.bs.DataMember == "") return;

            DataSet _ds = (DataSet)this.bs.DataSource;
            DataTable _dt = _ds.Tables[this.bs.DataMember];

            foreach (DataColumn _dc in _dt.Columns)
            {
                this.fieldDataGridViewTextBoxColumn.Items.Add(_dc.ColumnName);

                DataGridViewColumn _dgvc = null;
                if (_dc.AutoIncrement)
                    _dgvc = new DGVTextBoxColumn(DGVTextBoxColumnStyle.ID);
                else if (_dc.DataType == Type.GetType("System.DateTime"))
                    _dgvc = new DGVCalendarColumn();
                else if (_dc.DataType == Type.GetType("System.Decimal"))
                    _dgvc = new DGVTextBoxColumn(DGVTextBoxColumnStyle.Currency);

                ColumnBeforeAddEventArgs _e = new ColumnBeforeAddEventArgs(_dc, _dgvc);
                OnColumnBeforeAdd(_e);
                _dgvc = _e.GridColumn;

                if (_dgvc == null)
                    _dgvc = new DGVTextBoxColumn();

                _dgvc.DataPropertyName = _dc.ColumnName;
                _dgvc.HeaderText = _dc.ColumnName;

                this.dgv.Columns.Add(_dgvc);

                try
                {
                    IDGVColumn _idgvc = (IDGVColumn)_dgvc;
                    //if (_dgvc.GetType().IsAssignableFrom(typeof(IDGVColumn)))
                    //{
                    this.tableLayoutPanel1.RowCount++;
                    //this.tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
                    this.tableLayoutPanel1.Controls.Add(_idgvc.EditingLabel(), 0, this.tableLayoutPanel1.RowCount - 2);
                    this.tableLayoutPanel1.Controls.Add(_idgvc.EditingControl(), 1, this.tableLayoutPanel1.RowCount - 2);
                    //}
                }
                catch
                {
                }
            }
        }

        private void bn_RefreshItems(object sender, EventArgs e)
        {
        }

        private void bnAcceptChanges_Click(object sender, EventArgs e)
        {
            this.bs.EndEdit();
        }

        private void bnRejectChanges_Click(object sender, EventArgs e)
        {
            this.bs.CancelEdit();
        }

        private void ucData_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                this.bnFilter.Checked = false;
                this.bnViewGrid.Checked = true;
            }

            NotifyChange();
        }
        private void bnViewGrid_CheckedChanged(object sender, EventArgs e)
        {
            this.bnViewThumb.Checked = !this.bnViewGrid.Checked;
            this.scView.Panel1Collapsed = this.bnViewThumb.Checked;
            this.scView.Panel2Collapsed = !this.bnViewThumb.Checked;
        }

        private void bnViewThumb_CheckedChanged(object sender, EventArgs e)
        {
            this.bnViewGrid.Checked = !this.bnViewThumb.Checked;
        }

        private void bnFilter_CheckedChanged(object sender, EventArgs e)
        {
            this.scFilter.Panel1Collapsed = !this.bnFilter.Checked;
            if (this.bnFilterApply.Checked)
                this.bnFilterApply.Checked = false;
            this.bnFilterApply.Enabled = this.bnFilter.Checked;
        }

        private void bnFilterApply_CheckedChanged(object sender, EventArgs e)
        {
            if (this.bnFilterApply.Checked)
            {
                foreach (DataGridViewRow _dgvr in this.dgvFilter.Rows)
                {
                    DGVFilter f = (DGVFilter)_dgvr.DataBoundItem;
                    if (f != null)
                        this.bs.Filter = f.ToString();
                }
            }
            else
                this.bs.Filter = "";
        }

        private void bnFilterSave_Click(object sender, EventArgs e)
        {
            //if (this.settings == null)
            //    this.settings = new StringCollection();
            this.Settings.Add("key=value");
        }

        private void bnFilterLoad_Click(object sender, EventArgs e)
        {
            this.bnTest.Items.Clear();
            foreach (string s in this.Settings)
                this.bnTest.Items.Add(s);
            //this.bs.Filter =
        }
    }
}
